/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */
#include <gtest/gtest.h>
#define private public
#define protected public
#include "tiling_api.h"
#include "platform_stub.h"
#include "impl/matmul/tiling/math_util.h"
#include "impl/matmul/tiling/matmul_tiling_algorithm.h"
#include "tiling/platform/platform_ascendc.h"
using namespace AscendC;
using namespace std;
using namespace matmul_tiling;
class TestMatmulAPITiling : public testing::Test {
protected:
    static void SetUpTestCase() {}
    static void TearDownTestCase() {}
    virtual void SetUp() {}
    void TearDown() {}
};
TEST_F(TestMatmulAPITiling, TestMatmulApiTilngMultiCoreBTSCM)
{
    optiling::TCubeTiling tilingData;
    MultiCoreMatmulTiling tilingApi;
    tilingApi.SetDim(24);

    tilingApi.SetAType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
    tilingApi.SetBType(TPosition::TSCM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
    tilingApi.SetCType(TPosition::GM, CubeFormat::NZ, matmul_tiling::DataType::DT_FLOAT);
    tilingApi.SetBiasType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);

    tilingApi.SetOrgShape(65536, 64, 64);
    tilingApi.SetShape(-1, -1, -1);
    tilingApi.EnableBias(false);

    tilingApi.SetBufferSpace(-1, -1, -1);
    int64_t res = tilingApi.GetTiling(tilingData);
    tilingApi.PrintTilingData();
    EXPECT_EQ(res, 0);
}

TEST_F(TestMatmulAPITiling, TestMatmulApiTilngMultiCoreBTSCM2)
{
    optiling::TCubeTiling tilingData;
    MultiCoreMatmulTiling tilingApi;
    tilingApi.SetDim(24);

    tilingApi.SetAType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
    tilingApi.SetBType(TPosition::TSCM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
    tilingApi.SetCType(TPosition::GM, CubeFormat::NZ, matmul_tiling::DataType::DT_FLOAT);
    tilingApi.SetBiasType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);

    tilingApi.SetOrgShape(65536, 64, 64);
    tilingApi.SetShape(65536, 64, 64);
    tilingApi.EnableBias(false);

    tilingApi.SetBufferSpace(-1, -1, -1);
    int64_t res = tilingApi.GetTiling(tilingData);
    tilingApi.PrintTilingData();
    EXPECT_EQ(res, 0);
}

TEST_F(TestMatmulAPITiling, TestMatmulApiTilngMultiCoreCase)
{
    optiling::TCubeTiling tilingData;
    MultiCoreMatmulTiling tilingApi;
    tilingApi.SetDim(24);

    tilingApi.SetAType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
    tilingApi.SetBType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
    tilingApi.SetCType(TPosition::GM, CubeFormat::NZ, matmul_tiling::DataType::DT_FLOAT);
    tilingApi.SetBiasType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);

    tilingApi.SetOrgShape(4096, 5120, 4096);
    tilingApi.SetShape(-1, -1, -1);
    tilingApi.EnableBias(false);

    tilingApi.SetBufferSpace(-1, -1, -1);
    int64_t res = tilingApi.GetTiling(tilingData);
    tilingApi.PrintTilingData();
    EXPECT_EQ(res, 0);
}

TEST_F(TestMatmulAPITiling, TestMatmulApiTilngMultiCoreCaseWithTranspose)
{
    optiling::TCubeTiling tilingData;
    MultiCoreMatmulTiling tilingApi;
    tilingApi.SetDim(24);

    tilingApi.SetAType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16, true);
    tilingApi.SetBType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16, true);
    tilingApi.SetCType(TPosition::GM, CubeFormat::NZ, matmul_tiling::DataType::DT_FLOAT);
    tilingApi.SetBiasType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);

    tilingApi.SetOrgShape(4095, 5121, 4095);
    tilingApi.SetShape(-1, -1, -1);
    tilingApi.EnableBias(false);

    tilingApi.SetBufferSpace(-1, -1, -1);
    int64_t res = tilingApi.GetTiling(tilingData);
    tilingApi.PrintTilingData();
    EXPECT_EQ(res, 0);
}

TEST_F(TestMatmulAPITiling, L1CacheUBCase05BothCache)
{
    MultiCoreMatmulTiling tiling;
    tiling.SetDim(8);
    tiling.SetAType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
    tiling.SetBType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
    tiling.SetCType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT);
    tiling.SetBiasType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT);
    tiling.SetShape(-1, -1, -1);
    tiling.SetOrgShape(1024, 5120, 640);
    tiling.EnableBias(true);
    tiling.socVersion = platform_ascendc::SocVersion::ASCEND310P;
    tiling.SetMatmulConfigParams(1, true);
    optiling::TCubeTiling tilingData;
    int ret = tiling.GetTiling(tilingData);
    tiling.PrintTilingData();
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(tilingData.get_depthAL1CacheUB(), 1);
    EXPECT_EQ(tilingData.get_depthBL1CacheUB(), 2);
}